package vn.wallet.app.managedBean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import vn.wallet.app.domain.Currency;
import vn.wallet.app.domain.User;
import vn.wallet.app.helper.AppUtils;
import vn.wallet.app.helper.PageUtils;
import vn.wallet.app.helper.Validator;
import vn.wallet.app.service.ICurrencyService;
import vn.wallet.app.service.IUserService;

@ManagedBean(name = "registerBean")
@ViewScoped
public class RegisterUserBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Logger log = Logger.getLogger(this
			.getClass());
	private String title;
	private String username;
	private String password;
	private List<Currency> lstCurrency;
	private int selectedCurrencyID;
	private Currency selectedCurrency;

	@ManagedProperty(value = "#{currencyService}")
	private ICurrencyService currencyService;

	@ManagedProperty(value = "#{userService}")
	private IUserService userService;

	// ----------------- properties

	@PostConstruct
	public void initData() {
		try {
			lstCurrency = currencyService.getAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void saveItem() {
		try {
			if (validate()) {
				User user = new User();
				user.setTitle(title);
				user.setUsername(username);
				user.setEmail(username);
				user.setPassword(AppUtils.encryptPassword(password));
				user.setCurrency(selectedCurrency);
				user.setEmailConfirm(true); // wait for email function
				user.setStatus(true); // wait for email function
				user.setRegisterDate(new Date());
				user.setIpRegistration(AppUtils.getRequestIp());

				userService.save(user);

				PageUtils.logSuccess("frmRegister",
						"New user added successfully");
			}
		} catch (Exception ex) {
			PageUtils.logFailed(ex.getMessage());
			log.error(ex);
		}

	}

	private boolean validate() {
		boolean passed = true;
		try {
			if (StringUtils.hasText(getUsername())) {
				if (!Validator.isEmailAddress(getUsername())) {
					PageUtils.logFailed("frmRegister:msg_register",
							"Invalid email");
					passed = false;
				}
			}
			/*
			 * if (StringUtils.isNotEmpty(getPassword())) { if
			 * (!Validator.isValidPassword(password)) {
			 * PageUtils.logFailed("frmRegister", "Invalid password"); passed =
			 * false; } }
			 */
			if (userService.getByUsername(username) != null) {
				PageUtils.logFailed("frmRegister:msg_register",
						"User is exist!");
				passed = false;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}

		return passed;
	}

	// ----------------- Getter & Setter
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Currency> getLstCurrency() {
		return lstCurrency;
	}

	public void setLstCurrency(List<Currency> lstCurrency) {
		this.lstCurrency = lstCurrency;
	}

	public Currency getSelectedCurrency() {
		return selectedCurrency;
	}

	public void setSelectedCurrency(Currency selectedCurrency) {
		this.selectedCurrency = selectedCurrency;
	}

	public ICurrencyService getCurrencyService() {
		return currencyService;
	}

	public void setCurrencyService(ICurrencyService currencyService) {
		this.currencyService = currencyService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public int getSelectedCurrencyID() {
		return selectedCurrencyID;
	}

	public void setSelectedCurrencyID(int selectedCurrencyID) {
		this.selectedCurrencyID = selectedCurrencyID;
		for (Currency cur : lstCurrency) {
			if (selectedCurrencyID == cur.getId()) {
				setSelectedCurrency(cur);
				break;
			}
		}
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
}
